package com.erp.module.pieceworkGrouping.PieceworkGroupingXz

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PieceworkGroupingNameBean
import com.erp.bean.PieceworkcollBean
import com.erp.module.pieceworkGrouping.PieceworkGroupingContract
import com.erp.module.pieceworkGrouping.PieceworkGroupingM
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class PieceworkGroupingXzAcitivityP(var V : PieceworkGroupingContract.ProductionGroupingView, var context: Context, var mlist : MutableList<PieceworkGroupingNameBean>): PieceworkGroupingContract.ProductionGroupingKP {
    var md = PieceworkGroupingM()

    var gangweinames = ""
    override fun getGroupingNames() {
        var Ids: ArrayList<String> = ArrayList<String>()
        var Names: ArrayList<String> = ArrayList<String>()
        if(gangweinames == ""){
            md.getGroupingNames(context,object : OnUserDataInterface{
                override fun onUserData(data: String?) {
                    if (data == null) {
                        return
                    }
                    gangweinames = data
                    gangweinamejsons(data,Ids,Names)
                    V.getGroupingNamesOk(Ids,Names)
                }

                override fun onNot(throwable: String?) {
                    V.getGroupingNamesNo()
                }

            })
        }else{
            gangweinamejsons(gangweinames,Ids,Names)
            V.getGroupingNamesOk(Ids,Names)
        }

    }

    fun gangweinamejsons(data : String,ids: ArrayList<String>, names: ArrayList<String>){
        val jo = JSONObject(data)
        val joo = JsonUtil.getjovolue(jo,"gangweinames")
        val keys = joo.keys()
        while (keys.hasNext()) {
            val key = keys.next() as String
            ids.add(key)
            names.add(JsonUtil.getvolue(joo, key))
        }

    }

    override fun getGroupingList(gangwei_name:String,name:String,staff_name:String) {
        var map = hashMapOf<String,String>();
        map.put("gangwei_name", gangwei_name)
        map.put("name", name)
        map.put("staff_name", staff_name)
        md.getGroupingList(context,map,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                if (data != null) {
                    jsongangweidata(data)
                    V.getGroupingListOk()
                }
            }

            override fun onNot(throwable: String?) {
                V.getGroupingListNo()
            }

        })
    }

    //解析获取的分组数据
    fun jsongangweidata(data:String){
        try {
            mlist.clear()
            var jo = JSONObject(data)
            var joo = JsonUtil.getjovolue(jo,"data")
            var js = JsonUtil.getjsvolue(joo,"data")
            for (i in 0 until js.length()){
                var jodata: JSONObject = js.get(i) as JSONObject
                var bean = PieceworkGroupingNameBean()
                bean.gangwei_name = JsonUtil.getvolue(jodata,"gangwei_name")
                bean.kgg_name = JsonUtil.getvolue(jodata,"kgg_name")
                bean.remark = JsonUtil.getvolue(jodata,"remark")
                var jsstaff = JsonUtil.getjsvolue(jodata,"stafflist")
                var stafflist = "";
                for(j in 0 until jsstaff.length()){
                    var jostaff: JSONObject = jsstaff.get(j) as JSONObject
                    if(stafflist != ""){
                        stafflist += ","
                    }
                    stafflist += JsonUtil.getvolue(jostaff, "staff_name")
                }
                bean.stafflist = stafflist
                bean.kgg_id = JsonUtil.getvolue(jodata,"kgg_id")
                mlist.add(bean)
            }
        }catch (e : JSONException){
            Log.d("getGroupingListOk",e.toString())
            e.printStackTrace()
            return
        }
    }

    //解析分组岗位json
    fun jsondata(data : String){
        try {
            var jod = JSONObject(data)
            var js = JsonUtil.getjsvolue(jod,"gangweinames")
            var bean = PieceworkGroupingNameBean()
            for (i in 0 until js.length()){
                var jo: JSONObject = js.get(i) as JSONObject
                bean.kgg_id = JsonUtil.getvolue(jo,"kgg_id")
                bean.gangwei_name = JsonUtil.getvolue(jo,"kgg_id")
                bean.kgg_name = JsonUtil.getvolue(jo,"kgg_name")
                bean.remark = JsonUtil.getvolue(jo,"remark")
                bean.stafflist = JsonUtil.getvolue(jo,"stafflist")
                mlist.add(bean)
            }

        }catch (e : JSONException){
            Log.d("onSuccess",e.toString())
            e.printStackTrace()
            return
        }
    }
}